package code.easy.contentstate.demo

import androidx.annotation.DrawableRes
import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material.CircularProgressIndicator
import androidx.compose.material.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import code.easy.contentstate.EasyContentState
import code.easy.contentstate.ContentState

/**
 * 创建自己应用所对应的各种状态视图
 */
@Composable
fun MyContentState(
    contentState: ContentState,
    modifier: Modifier = Modifier,
    loading: (@Composable BoxScope.() -> Unit)? = null,
    empty: (@Composable BoxScope.() -> Unit)? = null,
    error: (@Composable BoxScope.() -> Unit)? = null,
    onErrorClick: (() -> Unit)? = null,
    content: @Composable BoxScope.() -> Unit
) {
    EasyContentState(
        contentState = contentState,
        modifier = modifier,
        loading = loading ?: { MyContentLoadingState() },
        empty = empty ?: { MyContentEmptyState() },
        error = error ?: { MyContentErrorState(onClick = onErrorClick ?: {}) },
        content = content
    )
}

/**
 * 加载中状态视图
 */
@Composable
fun MyContentLoadingState(text: String = "Loading...") {
    Box(Modifier.fillMaxSize()) {
        Column(modifier = Modifier.align(Alignment.Center)) {
            CircularProgressIndicator(modifier = Modifier.size(50.dp))
            Spacer(modifier = Modifier.height(10.dp))
            Text(text = text, color = Color.LightGray, fontSize = 12.sp)
        }
    }
}

/**
 * 空数据状态视图
 */
@Composable
fun MyContentEmptyState(
    text: String = "mo more data",
    textColor: Color = Color.Gray,
    drawableResId: Int = R.mipmap.ic_empty,
) {
    Box(Modifier.fillMaxSize()) {
        Column(
            Modifier.align(Alignment.Center),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(drawableResId),
                contentDescription = "",
                Modifier.size(80.dp, 80.dp)
            )
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = text, color = textColor, fontSize = 12.sp)
        }
    }
}

/**
 * 失败状态视图
 */
@Composable
fun MyContentErrorState(
    onClick: () -> Unit,
    @DrawableRes resId: Int = R.mipmap.ic_error,
    text: String = "request failed",
    textColor: Color = Color.Gray,
    buttonText: String = "try again",
    buttonTextColor: Color = Color.Green,
) {
    Box(Modifier.fillMaxSize()) {
        Column(
            Modifier.align(Alignment.Center),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            Image(
                painter = painterResource(resId),
                contentDescription = "",
                modifier = Modifier.size(80.dp, 80.dp)
            )
            Spacer(modifier = Modifier.height(12.dp))
            Text(text = text, color = textColor, fontSize = 12.sp)
            Spacer(modifier = Modifier.height(20.dp))
            Text(
                text = buttonText,
                color = buttonTextColor,
                modifier = Modifier.clickable { onClick() }
            )
        }
    }
}

